'use strict';
app.controller('profitSettingEditCtrl', ['$scope', '$modalInstance', 'toaster', '$state', 'profitSettingService',
    function($scope, $modalInstance, toaster, $state, profitSettingService) {

        // 打开窗体时初始化数据
        $scope.init = function(){
            profitSettingService.fetchByLevelAndType($scope).then(function(result){
                switch(result.status){
                    case 200:
                        $scope.editData.ratio = result.data.data.ratio;
                        break;
                    default :
                        toaster.pop('error', "初始待修改数据失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        }
        $scope.init();


        // 提交
        $scope.isEditModal=true;
        $scope.edit = function(){
            if(!$scope.isEditModal){
                return;
            }
            $scope.isEditModal = false;

            if($scope.editData.ratio > 100) {
                toaster.pop('waiting', "提示", '占比不能超过100%');
                $scope.isEditModal=true;
                return;
            }

            profitSettingService.update($scope).then(function(result){
                switch (result.status){
                    case 200:
                        toaster.pop('success', "提示", "操作成功");
                        $scope.cancel();
                        $state.reload('app.profitSetting');
                        $scope.isEditModal=true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        $scope.isEditModal=true;
                        break;
                }
            });
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
}]);